/**
 * 
 */
package account;

import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GraphicsConfiguration;
import java.awt.GridLayout;
import java.awt.HeadlessException;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

import misc.TextFieldFocusListener;

import database.Database;

/**
 * @author Kevin Hanson
 * 
 */
public class AccountRecordDialog extends JDialog implements ActionListener {
	private static Connection conn = null;
	private JTextField nameTextField = null;
	private JTextField uidTextField = null;
	private JTextField passwdTextField = null;
	private JTextField siteTextField = null;
	private JTextField commentTextField = null;
//	private JTextField siteTextField = null;
	private JButton newRecordButton = null;

	public AccountRecordDialog(JFrame parent, String title) {
		super(parent, title, true);
		if (parent != null) {
			Dimension parentSize = parent.getSize();
			Point p = parent.getLocation();
			setLocation(p.x + parentSize.width / 2, p.y + parentSize.height / 2);
		}

		conn = Database.getConnection();
		createGUI();
	}

	private void createGUI() {
		int WIDTH = 200, HEIGHT = 160;
		setSize(WIDTH, HEIGHT);
		Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
		setLocation((d.width - WIDTH) / 2, (d.height - HEIGHT) / 2);
		Container pane = getContentPane();
		pane.setLayout(new GridLayout(6, 2));

		nameTextField = new JTextField();
		nameTextField.setSize(new Dimension(60, 20));
		nameTextField.addFocusListener(new TextFieldFocusListener(nameTextField));
		uidTextField = new JTextField();
		uidTextField.setSize(new Dimension(60, 20));
		uidTextField.addFocusListener(new TextFieldFocusListener(uidTextField));
		passwdTextField = new JTextField();
		passwdTextField.setSize(new Dimension(60, 20));
		passwdTextField.addFocusListener(new TextFieldFocusListener(passwdTextField));
		siteTextField = new JTextField();
		siteTextField.setSize(new Dimension(60, 20));
		siteTextField.addFocusListener(new TextFieldFocusListener(siteTextField));
		commentTextField = new JTextField();
		commentTextField.setSize(new Dimension(60, 20));
		commentTextField.addFocusListener(new TextFieldFocusListener(commentTextField));

		newRecordButton = new JButton("New");
		newRecordButton.addActionListener(this);

		pane.add(new JLabel("Name"));
		pane.add(nameTextField);
		pane.add(new JLabel("UID"));
		pane.add(uidTextField);
		pane.add(new JLabel("Password"));
		pane.add(passwdTextField);
		pane.add(new JLabel("Site"));
		pane.add(siteTextField);
		pane.add(new JLabel("Comment"));
		pane.add(commentTextField);
		pane.add(newRecordButton);
	}

	private boolean saveRecord() {
		String text = null;
		try {
			PreparedStatement prep = conn
					.prepareStatement("INSERT INTO account VALUES(null, ?, ?, ?, ?, ?);");
			text = nameTextField.getText();
			prep.setString(1, text.equals(TextFieldFocusListener.defaultString) ? "" : text);
			text = uidTextField.getText();
			prep.setString(2, text.equals(TextFieldFocusListener.defaultString) ? "" : text);
			text = passwdTextField.getText();
			prep.setString(3, text.equals(TextFieldFocusListener.defaultString) ? "" : text);
			text = siteTextField.getText();
			prep.setString(4, text.equals(TextFieldFocusListener.defaultString) ? "" : text);
			text = commentTextField.getText();
			prep.setString(5, text.equals(TextFieldFocusListener.defaultString) ? "" : text);
			prep.execute();
			if (!conn.getAutoCommit())
				conn.commit();
		} catch (SQLException ex) {
			System.out.print(ex.getMessage());
			ex.printStackTrace();
			System.exit(-1);
		}

		return true;
	}

	public String getAccountInfo(String name) {
		if (name == "name")
			return nameTextField.getText();
		else if (name == "uid")
			return uidTextField.getText();
		else if (name == "passwd")
			return passwdTextField.getText();
		else if (name == "site")
			return siteTextField.getText();
		else if (name == "comment")
			return commentTextField.getText();
		return "";
	}

	public JButton getNewRecordButton() {
		return newRecordButton;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		saveRecord();
		setVisible(false);
	}
}
